package tech.aistar.corejava.day09.strings;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:可变的字符串,线程不安全的,拼接性能最高
 * @date 2019/7/25 0025
 */
public class StringBuilderDemo {
    public static void main(String[] args) {
        //利用String对象来构建一个StringBuilder对象
        //类型的转换
        //java.lang.StringBuider提供的StringBuilder(String str);
        //作用:将字符串类型转成StringBuilder类型

        //为什么转换?
        //里面是没有"串池"的概念!!!!
        StringBuilder builder = new StringBuilder("xyz");
        //StringBuilder x = "abc";

        //获取长度
        //System.out.println(builder.length());//1

       // System.out.println(builder);

        //字符串的拼接 - 由于它是可变的字符串
        builder.append("abc");//对原来的"xyz"对象进行直接的拼接操作

        System.out.println(builder);//xyzabc

        //不一样的地方 - 容量
        System.out.println(builder.capacity());//19

        //默认的容量是16
        System.out.println(new StringBuilder().capacity());//16

        //增,删除
        StringBuilder bs = new StringBuilder("java");

        //删除指定下标处的值,返回一个StringBuilder - 被删除之后的值
        StringBuilder delStr = bs.deleteCharAt(2);

        System.out.println("delStr:"+delStr);//jaa

        System.out.println(bs);//java

        //删除 StringBuilder delete(int start,int end);// [start,end);

        System.out.println(new StringBuilder("abcde").delete(1,3));//ade

        StringBuilder bu = new StringBuilder("pthon");

        bu.insert(1,"y");

        System.out.println(bu);//python

        //字符串的反转...
        StringBuilder binary = new StringBuilder("0101");

        System.out.println(binary.reverse());

        //如何将StringBuilder转换成String类型

        String bt = binary.toString();
        System.out.println(bt);

    }
}
